//
//  AddMooreDollViewController.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/1.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "AddMooreDollViewController.h"
#import "WinkAlertView.h"
#import "TelephonyInfo.h"
#import "HowToUseOnePageCell.h"
#import "HowToUseOtherCell.h"
#import "HowToUseMDViewController.h"
@interface AddMooreDollViewController ()

@end

@implementation AddMooreDollViewController

- (IBAction)startAddMooreDoll:(id)sender {
    
    
    //連上娃娃 就不能取消
    //開始新增娃娃 先檢查是不是在wifi 環境下面
    if ([[AFNetworkReachabilityManager sharedManager] isReachableViaWiFi]) {
        
        //取得目前的wifi 名稱
        NSString *wifiName = [TelephonyInfo currentWifiName];

        //檢查 如果使用者是連線到 娃娃的APMD 要先跟他說 先連上自己的wifi 而不是娃娃的MDAP
        if (![wifiName hasPrefix:@"MDAP"]) {
            
            //儲存使用者目前Wifi 名稱
            [[NSUserDefaults standardUserDefaults]setValue:wifiName forKey:@"userWifi"];
            //取得 getTuid
            [self.startGetTUID show];
        }else
        {
            [WinkAlertView _showAlertViewWithTitle:@"" content:@"請先連上您的Wifi環境(非娃娃的:MDAP XXXX)" cancelButtonTitle:@"確認" showTime:3.0f];
        }

    }else //不是在wifi 環境下 就先警告
    {
        [WinkAlertView _showAlertViewWithTitle:@"" content:@"請確認您在有Wifi的環境下" cancelButtonTitle:@"確認" showTime:3.0f];
//        HowToUseMDViewController *howToUseMDVC = [self.storyboard instantiateViewControllerWithIdentifier:@"HowToUseMDViewController"];
//        [self.navigationController pushViewController:howToUseMDVC animated:YES];

    }
    
}

- (void)getTuid
{
    self.afManager = [AFHTTPRequestOperationManager manager];
    
    self.afManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    self.afManager.responseSerializer.acceptableContentTypes = [self.afManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
    
    //提出國碼加帳號 密碼
    NSString *account = [TelephonyInfo accountAddCountryCode];
    NSString *password = [[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
    
    NSDictionary *parameters = @{@"category":@"1",@"type":@"add",@"secret":@"openfire",@"username":account,@"password":password};
    
    NSLog(@"%@",parameters);

    
        [self.afManager POST:MD_SERVER_API parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSLog(@"JSON: %@", responseObject);
            
            NSString *responseResult = [NSString stringWithFormat:@"%@",responseObject[@"result"]];
            //成功
            if ([responseResult isEqualToString:@"0"]) {
                
                //loading 結束
                
                //跳到下一畫面
                HowToUseMDViewController *howToUseMDVC = [self.storyboard instantiateViewControllerWithIdentifier:@"HowToUseMDViewController"];
                howToUseMDVC.tuid = responseObject[@"tuid"];
                
                [self.navigationController pushViewController:howToUseMDVC animated:YES];

            }else
            {
                
                [WinkAlertView _showAlertViewWithTitle:@"失敗" content:responseObject[@"msg"] cancelButtonTitle:@"確認" showTime:3.0f];
                
            }
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
            
            
            [WinkAlertView _showAlertViewWithTitle:@"失敗" content:@"網路連結失敗" cancelButtonTitle:@"確認" showTime:3.0f];
            
            
        }];

    

}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.startGetTUID= [[UIAlertView alloc]initWithTitle:@"注意" message:@"一旦開始請勿關閉本程式，如您途中終止程式請務必重新請動娃娃" delegate:self cancelButtonTitle:@"稍後再設定" otherButtonTitles:@"確認", nil];

}

-(void)viewWillAppear:(BOOL)animated
{
    
}
-(void)viewDidAppear:(BOOL)animated
{

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (self.startGetTUID == alertView) {
        if (buttonIndex==1) {
            [self getTuid];
        }
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
